package top.yangyitao.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import top.yangyitao.dto.OrganizationDto;
import top.yangyitao.dto.ResultDto;
import top.yangyitao.dto.TableDto;
import top.yangyitao.entity.Organization;
import top.yangyitao.service.OrganizationService;

@RestController
public class OrganizationController {

	@Autowired
	OrganizationService organizationService;
	
	@RequiresAuthentication
	@RequestMapping("/getAllOrganization")
	public List<OrganizationDto> getAllOrganization() {
		List<OrganizationDto> organizationDtos = new ArrayList<OrganizationDto>();
		List<Organization> organizations = organizationService.findAll();
		organizations.forEach(organization->organizationDtos.add(new OrganizationDto().convert(organization)));
		return organizationDtos;
	}
	
	@RequiresPermissions("organization_html")
	@RequestMapping("/organization_html")
	public ModelAndView organization_html() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/organization.html");
		return mv;
	}
	
	@RequiresPermissions("organization_html")
	@RequestMapping("/addOrganization")
	public ResultDto addOrganization(String name) {
		if(organizationService.findByName(name)!=null)
			return new ResultDto("机构已存在",null,2);
		Organization organization = new Organization();
		organization.setName(name);
		organizationService.save(organization);
		return new ResultDto("添加成功",null,0);
	}
	
	@RequiresPermissions("organization_html")
	@RequestMapping("/upOrganization")
	public ResultDto upOrganization(long id,String name) {
		Organization organization = organizationService.getOne(id);
		if(organization == null) {
			return new ResultDto("机构不存在!",null,2);
		}
		organization.setName(name);
		organizationService.save(organization);
		return new ResultDto("更新成功",null,0);
	}
	
	@RequiresPermissions("organization_html")
	@RequestMapping("/delOrganization")
	public ResultDto delOrganization(String ids) {
		String[] idArray = ids.split(",");
		List<Organization> organizations = new ArrayList<Organization>();
		for(String id : idArray) {
			Organization organization = new Organization();
			organization.setId(new Long(id));
			organizations.add(organization);
		}
		organizationService.delete(organizations);
		return new ResultDto("删除成功",null,0);
	}
	
	@RequiresPermissions("organization_html")
	@RequestMapping("/getOrganizationPage")
	public TableDto<OrganizationDto> getOrganizationPage(Integer pageNumber,Integer pageSize,String search) {
		Page<Organization> pageResult = organizationService.getOrganizationPage(pageNumber, pageSize, search);
		List<Organization> organizations = pageResult.getContent();
		List<OrganizationDto> organizationDtos = new ArrayList<OrganizationDto>();
		organizations.forEach(organization->organizationDtos.add(new OrganizationDto().convert(organization)));
		return new TableDto<OrganizationDto>(organizationDtos,pageResult.getTotalElements());
	}
}
